//
//  SigningProcessController.m
//  LuCaiZhu
//
//  Created by Stella on 2018/9/25.
//
#import "ProcessViewCell.h"
#import "Header.h"
#import "OrderDetailController.h"
#import "ReadingFileController.h"
#import "UIColor+NN.h"
#import "CKPuslichTool.h"
#import "HandleNetProduct.h"
#import "FinishedController.h"
#import "SigningProcessController.h"
#import "SignAgreementController.h"

@interface SigningProcessController ()
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (strong, nonatomic)NSArray *datas;
@end

@implementation SigningProcessController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电子签约流程";
    
    _mTableView.backgroundColor = [UIColor colorWithHexString:@"f4f4f4"];
  
    [self getProcessDatas];
}

- (void)getProcessDatas{
    [HandleNetProduct getSigningProcessforBookingID:_bookingId Success:^(NSArray *data) {
        _datas = [NSArray arrayWithArray:data];
        [self.mTableView reloadData];
//        [self goFinishedView];
    } Failure:^(NSString *errorInfo) {
        
    }];
}

- (void)MyButtonPresss:(UIButton *)sender {
    
    NSDictionary *dic = _datas[sender.tag];
    NSString *text = dic[@"StepName"];
    NSLog(@"_datas[sender.tag]===%@",_datas[sender.tag]);
    if ([text rangeOfString:@"订单详情"].length > 0) {
        //去订单详情
        OrderDetailController *vc = (OrderDetailController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"OrderDetailController"];
        vc.stepDic = dic;
        vc.returnChangeOK = ^{
            
            [self getProcessDatas];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if([text rangeOfString:@"电子签名约定书"].length > 0){
        SignAgreementController *vc = (SignAgreementController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"SignAgreementController"];
        vc.stepDic = dic;
        vc.returnSubmitOK = ^{
            [self getProcessDatas];
        };
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        ReadingFileController * vc = (ReadingFileController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"ReadingFileController"];
        
        vc.SigningStatus = _SigningStatus;
        vc.stepDic = dic;
        vc.returnChangeOK = ^{
            if ([dic[@"IsSMS"] isEqualToString:@"Yes"] && [self.SigningStatus isEqualToString:@"A"]) {
                [self goFinishedView];
            }
            [self getProcessDatas];
        };
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)goFinishedView{
    FinishedController *vc1 = (FinishedController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"FinishedController"];
    vc1.returnOK = ^{
        if (self.returnBlock) {
            self.returnBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    };
    [vc1 setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [vc1 setModalPresentationStyle:UIModalPresentationOverFullScreen];
    [self presentViewController:vc1 animated:YES completion:nil];
}


#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 89;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ProcessViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ProcessViewCell"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    NSDictionary *dic = _datas[indexPath.row];
    if (indexPath.row == 0) {
        cell.topline.hidden =  YES;
        cell.downline.hidden =  NO;
    }else if (indexPath.row == _datas.count - 1) {
        cell.topline.hidden =  NO;
        cell.downline.hidden =  YES;
    }else{
        cell.topline.hidden =  NO;
        cell.downline.hidden =  NO;
    }
    cell.status = dic[@"Status"];
    if ([dic[@"Status"] isEqualToString:@"完成"]) {
        cell.name.text = dic[@"StepName2"];
    }else{
        if ([dic[@"StepName"] rangeOfString:@"份额确认书"].length > 0) {
            cell.name.text = [NSString stringWithFormat:@"%@\n(待管理人出具生成)",dic[@"StepName"]];
        }else {
            cell.name.text = dic[@"StepName"];
        }
    }
    cell.myButton.tag = indexPath.row;
    [cell.myButton addTarget:self action:@selector(MyButtonPresss:) forControlEvents:UIControlEventTouchDown];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}


@end
